
package com.harusource.di.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.widget.SimpleExpandableListAdapter;

public class HaruUtil {
    private static final String EXPANDABE_KEY1 = "GROUP";

    private static final String EXPANDABE_KEY2 = "CHILD";

    public static SimpleExpandableListAdapter getSimpleExpandableListAdapter(Context context,
            String[] group_data, int group_layout, int group_text_id, String[][][] child_data,
            int child_layout, int[] child_text_id) {

        if (group_data.length != child_data.length)
            throw new RuntimeException();

        List<Map<String, String>> group = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> child = new ArrayList<List<Map<String, String>>>();

        for (int i = 0; i < group_data.length; i++) {
            Map<String, String> mapg = new HashMap<String, String>();
            group.add(mapg);
            mapg.put(EXPANDABE_KEY1, group_data[i]);

            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            for (int j = 0; j < child_data[i].length; j++) {
                Map<String, String> mapc = new HashMap<String, String>();
                list.add(mapc);
                mapc.put(EXPANDABE_KEY1, child_data[i][j][0]);
                mapc.put(EXPANDABE_KEY2, child_data[i][j][1]);
            }
            child.add(list);
        }

        return new SimpleExpandableListAdapter(context, group, group_layout, new String[] {
            EXPANDABE_KEY1
        }, new int[] {
            group_text_id
        }, child, child_layout, new String[] {
                EXPANDABE_KEY1, EXPANDABE_KEY2
        }, child_text_id);
    }

    public static SimpleExpandableListAdapter getSimpleExpandableListAdapter(Context context,
            String[] group_data, String[][][] child_data) {
        return getSimpleExpandableListAdapter(context, group_data,
                android.R.layout.simple_expandable_list_item_1, android.R.id.text1, child_data,
                android.R.layout.simple_expandable_list_item_2, new int[] {
                        android.R.id.text1, android.R.id.text2
                });
    }
}
